package insigne.kernel.sgbdinterface.basics;


import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Commande extends DBOperator{
	/* ############################### Fields ############################### */
	private int IDCommande;

	private java.sql.Date Date;

	/* ############################### Getters ############################## */
	public int getIDCommande(){
		return IDCommande;
	}

	public java.sql.Date getDate(){
		return Date;
	}

	/* ############################### Setters ############################## */
	public void setIDCommande(int newValue){
		this.IDCommande = newValue;
	}

	public void setDate(java.sql.Date newValue){
		this.Date = newValue;
	}

	/* ########################### CLASS CONSTRUCTORS ####################### */
	/** ATTENTION LA DATE NE SERT A RIEN, LA DATE INSEREE EST LA DATE ACTUELLE */
	public Commande(java.sql.Date Date) throws SQLException {
		openConnection();
		this.Date = Date;
		this.IDCommande = executeUpdate("INSERT INTO commande VALUES('DEFAULT',CURRENT_TIMESTAMP())");
		con.close();
	}

	public Commande(int IDCommande) throws SQLException {
		openConnection();
		ResultSet rs = executeSelect("SELECT * FROM commande WHERE IDCommande='"+IDCommande+"'");
		rs.next();
		this.IDCommande = rs.getInt("IDCommande");
		this.Date = rs.getDate("Date");
		con.close();
	}

	public Commande() {
		// TODO Auto-generated constructor stub
	}

	public ArrayList<Commande> getAllCommandes() throws SQLException {
		openConnection();
		ResultSet rs = executeSelect("SELECT * FROM commande");
		ArrayList<Commande> res = new ArrayList<Commande>();
		while (rs.next()){
			Commande tmp = new Commande();
			tmp.IDCommande = rs.getInt("IDCommande");
			tmp.Date = rs.getDate("Date");
			res.add(tmp);
		}
		con.close();
		return res;
	}
	/* ############################### methods ############################## */
	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return IDCommande + " " + Date.toString();
	}
}